Creating user control

To create User Control in C# select the “Windows Forms Control Library” icon from New Project Dialog Box.
Add some controls and add codes for the controls in User Control.
Example

Code
private void btnColour_Click(object sender, EventArgs e)
{
//this will change the color of form on which this control is placed.
this.ParentForm.BackColor= Color.Red;
}
Now once the control is created we need to build it in order to use it in other projects.
Using User Control in project
To use a user control in our project we need to add that control in the project. To add right-click on Toolbox and select “Choose Items”

A dialog box “Choose Toolbox Items” will appear. In that dialog box click the Browse button and select user control created earlier from the bin\debug folder of the user control project, then press Ok.

The user control will be added to the project toolbox.

Now we can use the user control.
Example
Screenshot after Execution

Color of the form changes on button click without doing any coding in the current form. On the button click code of the user, control is executed.
Leave Comment
2 Comments